/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.tunnel.db.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DepartmentMapper extends BaseMapper<DepartmentDO> {

    DepartmentDO selectByExternalId(String deptId);

    List<DepartmentDO> selectAllDepartments();

    int deleteByPrimaryKey(Integer id);

    DepartmentDO selectById(@Param("id") Integer id);

    DepartmentDO selectByCode(@Param("code") String code);

    List<DepartmentDO> selectByIdentitySourceId(@Param("identitySourceId") Integer identitySourceId);

    void batchDeleteById(@Param("idList") List<Integer> idList);

    List<DepartmentDO> selectByExternalIdList(@Param("externalIdList") List<String> externalIdList, @Param("identitySourceId") Integer identitySourceId);

    default List<DepartmentDO> selectListByParam(Integer parentId) {
        LambdaQueryWrapper<DepartmentDO> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DepartmentDO::getIsDeleted, OkrConstants.NOT_DELETED);
        queryWrapper.eq(DepartmentDO::getEnabled, OkrConstants.ENABLE);
        queryWrapper.eq(parentId != null, DepartmentDO::getParentId, parentId);
        return selectList(queryWrapper);
    }


    public default List<DepartmentDO> findChildByDeptId(Integer deptId) {
        LambdaQueryWrapper<DepartmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartmentDO::getParentId, deptId);
        queryWrapper.eq(DepartmentDO::getIsDeleted, OkrConstants.NOT_DELETED);
        return selectList(queryWrapper);
    }

    @Select("SELECT id FROM t_department d where is_deleted=0 and  parent_id=#{deptId} ")
    List<Integer> findChildByDeptIds(Integer deptId);

    @Select("SELECT * FROM t_department d where is_deleted=0 and  external_id=#{externalId} ")
    DepartmentDO findByExternalId(String externalId);


    @Select("SELECT * FROM t_department d where is_deleted=0 and  parent_id=#{parentId} ")
    List<DepartmentDO> findByParentId(Integer parentId);


    public default DepartmentDO findByDeptId(Integer deptId) {
        LambdaQueryWrapper<DepartmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartmentDO::getId, deptId);
        queryWrapper.eq(DepartmentDO::getIsDeleted, OkrConstants.NOT_DELETED);
        return selectOne(queryWrapper);
    }

    @Select("SELECT d.* FROM t_department d,t_department_user du WHERE d.id=du.department_id AND du.user_id=#{userId}")
    List<DepartmentDO> findByUserId(Integer userId);

}
